<!-- 待检任务的首页组件 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="检测类型" prop="inspectionType">
        <el-select v-model="queryParams.inspectionType" placeholder="请选择检测类型" clearable>
          <el-option label="出货检验单" value="1" />
          <el-option label="来料检验单" value="2" />
          <el-option label="过程检验单" value="3" />
          <el-option label="退料检验单" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="检测编码" prop="inspectionCode">
        <el-input
          v-model="queryParams.inspectionCode"
          placeholder="请输入检测编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList"
      border
      size="mini"
      style="width: 100%"
      :row-style="{height: '40px'}"
      :cell-style="{padding: '0px'}"
      :header-cell-style="{height: '40px', padding: '0px'}"
    >
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="检测类型" align="center" prop="inspectionType">
        <template slot-scope="scope">
          {{ formatInspectionType(scope.row.inspectionType) }}
        </template>
      </el-table-column>
      <el-table-column label="检测编码" align="center" prop="inspectionCode" />
      <el-table-column label="来源单号" align="center" prop="sourceNo" />
      <el-table-column label="检测数量" align="center" prop="checkQty" />
      <el-table-column label="检测结果" align="center" prop="checkResult">
        <template slot-scope="scope">
          <el-tag :type="getCheckResultTagType(scope.row.checkResult)">
            {{ formatCheckResult(scope.row.checkResult) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="getStatusTagType(scope.row.status)">
            {{ formatStatus(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="检测日期" align="center" prop="inspectDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inspectDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-promotion"
            @click="handleGoToInspection(scope.row)"
          >去执行检测单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listInspectionTask } from "@/api/qc/PendingTasks";

export default {
  name: "ZlPendingTasks",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待检任务表格数据
      taskList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inspectionType: null,
        inspectionCode: null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询待检任务列表 */
    getList() {
      this.loading = true;
      listInspectionTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看详情按钮操作 */
    handleView(row) {
      this.$alert(`待检任务详情：${JSON.stringify(row)}`, '详情', {
        confirmButtonText: '确定'
      });
    },
    /** 执行检测按钮操作 */
    handleExecute(row) {
      // 根据检测类型跳转到对应的检查单修改页面
      switch (row.inspectionType) {
        case '1': // 出货检验单
          this.$router.push({
            path: '/qc/OutgoingInspection',
            query: {
              mode: 'edit',
              id: row.sourceId || row.id
            }
          });
          break;
        case '2': // 来料检验单
          this.$router.push({
            path: '/qc/IncomingInspection',
            query: {
              mode: 'edit',
              id: row.sourceId || row.id
            }
          });
          break;
        case '3': // 过程检验单
          this.$router.push({
            path: '/qc/ProcesInspection',
            query: {
              mode: 'edit',
              id: row.sourceId || row.id
            }
          });
          break;
        case '4': // 退料检验单
          this.$router.push({
            path: '/qc/ReturnInspection',
            query: {
              mode: 'edit',
              id: row.sourceId || row.id
            }
          });
          break;
        default:
          this.$message.warning('未知的检测类型');
      }
    },
    /** 去执行检测单按钮操作 */
    handleGoToInspection(row) {
      // 只获取检验单编号，用作查询条件
      const inspectionCode = row.inspectionCode;

      // 如果没有检验单编号，则提示用户
      if (!inspectionCode) {
        this.$message.warning('该待检任务没有有效的检验单编号');
        return;
      }

      switch (row.inspectionType) {
        case '1': // 出货检验单
          this.$router.push({
            path: '/qc/OutgoingInspection',
            query: {
              inspectionCode: inspectionCode
            }
          });
          break;
        case '2': // 来料检验单
          this.$router.push({
            path: '/qc/IncomingInspection',
            query: {
              inspectionCode: inspectionCode
            }
          });
          break;
        case '3': // 过程检验单
          this.$router.push({
            path: '/qc/ProcesInspection',
            query: {
              inspectionCode: inspectionCode
            }
          });
          break;
        case '4': // 退料检验单
          this.$router.push({
            path: '/qc/ReturnInspection',
            query: {
              inspectionCode: inspectionCode
            }
          });
          break;
        default:
          this.$message.warning('未知的检测类型');
      }
    },
    /** 格式化检测类型显示 */
    formatInspectionType(type) {
      const typeMap = {
        '1': '出货检验单',
        '2': '来料检验单',
        '3': '过程检验单',
        '4': '退料检验单'
      };
      return typeMap[type] || type;
    },
    /** 格式化检测结果显示 */
    formatCheckResult(result) {
      const resultMap = {
        '1': '合格',
        '2': '不合格',
        '3': '待检'
      };
      return resultMap[result] || result;
    },
    /** 格式化单据状态显示 */
    formatStatus(status) {
      const statusMap = {
        '1': '草稿',
        '2': '已提交',
        '3': '已审核'
      };
      return statusMap[status] || status;
    },
    /** 获取检测结果标签类型 */
    getCheckResultTagType(result) {
      const typeMap = {
        '1': 'success',
        '2': 'danger',
        '3': 'info'
      };
      return typeMap[result] || '';
    },
    /** 获取状态标签类型 */
    getStatusTagType(status) {
      const typeMap = {
        '1': 'info',
        '2': 'primary',
        '3': 'success'
      };
      return typeMap[status] || '';
    }
  }
};
</script>

<style scoped>
/* 优化固定列样式 */
::v-deep .el-table__fixed-right {
  height: 100% !important;
}

::v-deep .el-table__fixed-left {
  height: 100% !important;
}

::v-deep .el-table__row td {
  height: 40px !important;
}

/* 确保所有单元格内容垂直居中 */
::v-deep .el-table .cell {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

/* 调整按钮单元格内容样式 */
::v-deep .el-table .cell .el-button {
  padding: 3px 0;
  margin: 0 3px;
}
</style>
